package com.zj.leetcode.medium;

import java.util.List;

/**
 * @program: algorithm
 * @description: 通过删除字母匹配到字典里最长单词
 * 524 longest-word-in-dictionary-through-deleting
 * @author: Zhang Bo
 * @create: 2022-01-14 17:05
 **/
public class LongestWordInDictionaryThroughDeleting {
    public static void main(String[] args) {
        Solution solution = new LongestWordInDictionaryThroughDeleting().new Solution();
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public String findLongestWord(String s, List<String> dictionary) {

            String res = "";
            for (String t : dictionary) {

                int i = 0, j = 0;
                while (i < t.length() && j < s.length()) {
                    if (t.charAt(i) != s.charAt(j)) {
                        ++j;
                    } else {
                        ++i;
                        ++j;
                    }
                }

                if (i == t.length()) {
                    if (t.length() > res.length() || (t.length() == res.length() && t.compareTo(res) < 0)) {
                        res = t;
                    }
                }

            }
            return res;

        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}